'use strict';

const Service = require('egg').Service;
const Mock = require('mockjs');
const dayjs = require('dayjs');
dayjs().format()

class CommentService extends Service {
    // 获取文章列表
    async getList() {
        const {
            ctx,
            app
        } = this;
        //     // 模拟数据插入表中
        //     const {list} = Mock.mock({
        //         "list|20":[
        //             {
        //                 "state|1":["已通过","未通过"],
        //                 name:"@ctitle(4,10)",
        //                 tel:/^[1][3-8]\d{9}$/,
        //                 original:"@ctitle(4,10)",
        //                 html:"@ctitle(4,10)",
        //                 create_time:Date.now(),
        //                 parent:"@ctitle(4,10)",
        //             }
        //         ]            
        //     });
        //     // 循环list
        //     list.forEach((item)=>{
        //         app.mysql.insert('comment',{...item});
        //     });
        //     return 'ok'
        const {
            pageSize,
            pageIndex,
            name,
            tel,
            state,
            article_id
        } = ctx.query;
        // 如果article_id存在,那么返回当前文章id的评论列表
        if (article_id) {
            const res = await app.mysql.select('comment', {
                where: {
                    article_id,
                }
            });
            return res
        }
        let sql = `select * from comment where 1=1 `;
        let countSql = `select count(id) as total from comment where 1=1 `
        if (name) {
            sql += `and name like "%${name}%" `
            countSql += `and name like "%${name}%" `
        }
        if (tel) {
            sql += `and tel like "%${tel}%" `
            countSql += `and tel like "%${tel}%" `
        }
        if (state) {
            sql += `and state="${state}" `
            countSql += `and state="${state}" `
        }
        if (pageSize && pageIndex) {
            sql += `limit ${(pageIndex-1)*pageSize},${pageSize} `
        }
        const res = await app.mysql.query(sql);
        const [{
            total
        }] = await app.mysql.query(countSql);
        return {
            data: res,
            total,
        }
    }
    // 获取一级评论
    async getFirstList() {
        const {
            ctx,
            app
        } = this;
        const {
            pageSize,
            pageIndex,
            article_id,
            page_id
        } = ctx.query;
        let row = {
            where: {
                parent_id: 0,
                state: "已通过"
            }
        };
        if (page_id) {
            row.where.page_id = page_id
        }
        if (article_id) {
            row.where.article_id = article_id;
        }
        if (pageSize && pageIndex) {
            row.limit = pageSize
            row.offset = (pageIndex - 1) * pageSize
        }
        // 返回所有已通过并且parent_id为0的数据
        let res = await app.mysql.select('comment', row);
        // 创建一个存放promise的数组
        let promiseArr = [];
        // 循环res
        res.forEach((item) => {
            // 添加promise任务
            promiseArr.push(app.mysql.select('comment', {
                where: {
                    state: "已通过",
                    parent_id: item.id
                }
            }))
        })
        // 执行所有promise任务
        await Promise.all(promiseArr).then(response => {
            // 根据结果给res每一个元素添加children
            response.forEach((item, index) => {
                res[index].children = item;
            })
        })
        return res
    }
    // 发布评论
    async publish() {
        const {
            ctx,
            app
        } = this;
        const {
            name,
            tel,
            bgColor,
            article_id,
            page_id,
            value,
            parent_name,
            parent_id
        } = ctx.request.body;
        if (page_id) {
            // 判读是否是一级评论
            if (parent_name) {
                // 不是一级评论
                const res = await app.mysql.insert('comment', {
                    state: "未通过",
                    name,
                    tel,
                    bgColor,
                    page_id,
                    original: value,
                    html: value,
                    create_time: Date.now(),
                    parent_name: parent_name,
                    parent_id: parent_id
                })
                return res
            } else {
                console.log('执行');
                // 是一级评论
                const res = await app.mysql.insert('comment', {
                    state: "未通过",
                    name,
                    tel,
                    bgColor,
                    page_id,
                    original: value,
                    html: value,
                    create_time: Date.now(),
                    parent_name: '无',
                    parent_id: 0
                })
                return res
            }
        } else {
            // 判读是否是一级评论
            if (parent_name) {
                // 不是一级评论
                const res = await app.mysql.insert('comment', {
                    state: "未通过",
                    name,
                    tel,
                    bgColor,
                    article_id,
                    original: value,
                    html: value,
                    create_time: Date.now(),
                    parent_name: parent_name,
                    parent_id: parent_id
                })
                return res
            } else {
                // 是一级评论
                const res = await app.mysql.insert('comment', {
                    state: "未通过",
                    name,
                    tel,
                    bgColor,
                    article_id,
                    original: value,
                    html: value,
                    create_time: Date.now(),
                    parent_name: '无',
                    parent_id: 0
                })
                return res
            }
        }

    }
    // 更新评论
    async update() {
        const {
            ctx,
            app
        } = this;
        const {
            id,
            state
        } = ctx.request.body;
        const res = await app.mysql.update('comments',{
            id,
            state,
        })
        return res
    }
    // 删除评论
    async delComment(){
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.request.body;
        const res = await app.mysql.delete('comment', {
            id
        });
        return res
    }
}

module.exports = CommentService;